<template>
	<view class="main-content" v-show="isShow" @touchmove.stop.prevent="">
		<!-- 签字canvas -->
		<canvas class="mycanvas" id="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove"
			@touchend="touchend"></canvas>
		<!-- 旋转canvas -->
		<canvas class="mycanvas2"
			:style="{ 'z-index': -1, width: `${screenWidth}px`, height: `${(screenWidth * screenWidth) / screenHeight}px` }"
			id="rotatCanvas" canvas-id="rotatCanvas"></canvas>
		<cover-view v-show="!hasDh" class="hint-text"> 请在此处签名 </cover-view>
		<cover-view class="button-line">
			<cover-view class="button-style" :class="{'save-button' : hasDh}" @tap="finish">保存</cover-view>
			<cover-view class="button-style" :class="{'clear-button' : hasDh}" @tap="clear">清除</cover-view>
			<cover-view class="cancel-button" @tap="hide">关闭</cover-view>
		</cover-view>
	</view>
</template>

<script>
	export default {
		name: "signature",
		data() {
			return {
				ctx: null, //绘图图像
				points: [], //路径点集合
				isShow: false,
				screenWidth: '',
				screenHeight: '',
				hasDh: false,
				myShow: true
			};
		},
		mounted() {
			this.createCanvas();
			uni.getSystemInfo({
				success: e => {
					this.screenWidth = e.screenWidth;
					this.screenHeight = e.screenHeight;
				}
			});
		},
		methods: {
			show() {
				this.isShow = true;
				this.$nextTick(()=> {
					this.createCanvas();
					this.clear();
				})
			},
			hide() {
				this.ctx = null
				this.hasDh = false
				this.isShow = false;
				this.$emit('close')
			},
			//创建并显示画布
			createCanvas() {
				this.ctx = uni.createCanvasContext('mycanvas', this); //创建绘图对象
				//设置画笔样式
				this.ctx.setStrokeStyle('#000');
				//设置线的宽度
				this.ctx.setLineWidth(5);
				//设置线两端端点样式更加圆润
				this.ctx.setLineCap('round');
				//设置两条线连接处更加圆润
				this.ctx.setLineJoin('round');
			},
			//触摸开始，获取到起点
			touchstart(e) {
				let startX = e.changedTouches[0].x;
				let startY = e.changedTouches[0].y;
				let startPoint = {
					X: startX,
					Y: startY
				};
				this.hasDh = true;
				this.points.push(startPoint);
				//每次触摸开始，开启新的路径
				this.ctx.beginPath();
			},
			//触摸移动，获取到路径点
			touchmove(e) {
				let moveX = e.changedTouches[0].x;
				let moveY = e.changedTouches[0].y;
				let movePoint = {
					X: moveX,
					Y: moveY
				};
				this.points.push(movePoint); //存点
				let len = this.points.length;
				if (len >= 2) {
					this.draw(); //绘制路径
				}
			},
			// 触摸结束，将未绘制的点清空防止对后续路径产生干扰
			touchend() {
				this.points = [];
			},
			/* ***********************************************
				#   绘制笔迹
				#	1.为保证笔迹实时显示，必须在移动的同时绘制笔迹
				#	2.为保证笔迹连续，每次从路径集合中区两个点作为起点（moveTo）和终点(lineTo)
				#	3.将上一次的终点作为下一次绘制的起点（即清除第一个点）
				************************************************ */
			draw() {
				let point1 = this.points[0];
				let point2 = this.points[1];
				this.points.shift();
				this.ctx.moveTo(point1.X, point1.Y);
				this.ctx.lineTo(point2.X, point2.Y);
				this.ctx.stroke();
				this.ctx.draw(true);
			},
			//清空画布
			clear() {
				this.hasDh = false;
				this.ctx.setStrokeStyle('#000');
				//设置线的宽度
				this.ctx.setLineWidth(5);
				//设置线两端端点样式更加圆润
				this.ctx.setLineCap('round');
				//设置两条线连接处更加圆润
				this.ctx.setLineJoin('round');
				this.ctx.clearRect(0, 0, this.screenWidth, this.screenHeight);
				this.ctx.draw(true);
			},
			//完成绘画并保存到本地
			finish() {
				if (!this.hasDh) {
					uni.showToast({
						title: '请先签字',
						icon: 'none'
					});
					return;
				}
				uni.showModal({
					title: '保存提示',
					content: '保存后不可更改，是否确定保存签名？',
					success: (res) => {
						if (res.confirm) {
							uni.canvasToTempFilePath({
									canvasId: 'mycanvas',
									success: res => {
										this.myShow = false
										this.rotat(res.tempFilePath);
									},
									complete: com => {}
								},
								this
							);
						}
					}
				})
			},
			// 将图片选装
			rotat(e) {
				uni.showLoading({ mask: true, title: 'loading···' })
				let rotatCtx = uni.createCanvasContext('rotatCanvas', this); //创建绘图对象
				// 重新定位中心点
				rotatCtx.translate(0, (this.screenWidth * this.screenWidth) / this.screenHeight);
				// 将画布逆时针旋转90度
				rotatCtx.rotate((270 * Math.PI) / 180);
				// 将签字图片绘制进入Canvas
				rotatCtx.drawImage(e, 0, 0, (this.screenWidth * this.screenWidth) / this.screenHeight, this.screenWidth);
				// 保存后旋转后的结果
				rotatCtx.draw(true);
				setTimeout(() => {
					// 生成图片并回调
					uni.canvasToTempFilePath({
							canvasId: 'rotatCanvas',
							success: val => {
								this.$emit('confirm', val.tempFilePath);
								setTimeout(() => {
									this.hide();
								}, 200);
							},
							complete: com => {
								this.myShow = true
								uni.hideLoading()
							}
						},
						this
					);
				}, 500);
			},
		}
	}
</script>

<style lang="scss">
	.main-content {
		width: 100vw;
		height: 100vh;
		background-color: red;
		position: fixed;
		top: 0rpx;
		left: 0rpx;
		z-index: 9999;
		overflow: hidden;
	}
	
	.mycanvas {
		width: 100vw;
		height: 100vh;
		background-color: #efefef;
		position: fixed;
		left: 0rpx;
		top: 0rpx;
		z-index: 2;
	}
	
	.mycanvas2 {
		width: 100vw;
		height: 100vh;
		position: fixed;
		left: -100000000rpx;
		top: -1000000000rpx;
		z-index: 2;
		background-color: #efefef;
	}
	
	.hint-text{
		transform: rotate(90deg);
		position: fixed;
		bottom: 50%;
		left: 40%;
		display: flex;
		color: #999999;
		align-items: center;
		justify-content: center;
		z-index: 999;
	}

	.button-line {
		transform: rotate(90deg);
		position: fixed;
		bottom: 170rpx;
		left: -100rpx;
		width: 340rpx;
		height: 50rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		z-index: 999;
	}

	.button-style {
		color: #ffffff;
		width: 100rpx;
		height: 60rpx;
		text-align: center;
		line-height: 60rpx;
		border-radius: 10rpx;
		background-color: #999999;
	}

	.save-button {
		@extend .button-style;
		background-color: #0055ff !important;
	}

	.clear-button {
		@extend .button-style;
		background-color: #ffa500 !important;
	}

	.cancel-button {
		@extend .button-style;
		background-color: #e10b2b !important;
	}
</style>